﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DataUpload;
using Logistics.DataManage.Local;
using Logistics.DataManage;

namespace logistics
{
    public partial class CheckUser : Form
    {
        private string custom_ID = string.Empty;
        private string outletID = string.Empty;
        public string Custom_ID
        {
            get { return custom_ID; }
            set { custom_ID = value; }
        }

        public CheckUser(string _outletID,string number)
        {
            InitializeComponent();
            DataSet ds = new DataSet();
            if (String.IsNullOrEmpty(number))
            {
                ds = CustomerDAO.GetCustomersByOutletId(_outletID);
            }
            else
            {
                ds = CustomerDAO.GetCustomersByOutletId(_outletID, number);
            }
            MainFormInitalize(dataGridView1, ds.Tables[0]);
            outletID = _outletID;
        }


        public void MainFormInitalize(DataGridView dgvWaybillInfor, DataTable dt)
        {
            dgvWaybillInfor.AllowUserToAddRows = false;
            dgvWaybillInfor.Rows.Clear();
            if (dt.Rows.Count > 0)
            {
                dgvWaybillInfor.AutoGenerateColumns = false;
                dgvWaybillInfor.RowCount = dt.Rows.Count;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dgvWaybillInfor.Rows[i].Cells["num"].Value = (i + 1).ToString();
                    dgvWaybillInfor.Rows[i].Cells["CustomID"].Value = dt.Rows[i]["CustomID"].ToString();
                    dgvWaybillInfor.Rows[i].Cells["CustomName"].Value = dt.Rows[i]["CustomName"].ToString();
                    dgvWaybillInfor.Rows[i].Cells["CustomTel"].Value = dt.Rows[i]["CustomTel"].ToString();
                    dgvWaybillInfor.Rows[i].Cells["CustomMobile"].Value = dt.Rows[i]["CustomMobile"].ToString();
                    dgvWaybillInfor.Rows[i].Cells["BankID"].Value = dt.Rows[i]["BankID"].ToString();
                    dgvWaybillInfor.Rows[i].Cells["Address"].Value = dt.Rows[i]["Address"].ToString();
                }
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                string ID = this.dataGridView1.Rows[e.RowIndex].Cells[1].EditedFormattedValue.ToString();
                this.Custom_ID = ID;
                this.Close();
            }
        }

        //按数字选择行    
        private void txtCheckRow_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() != "\b" && !(char.IsDigit(e.KeyChar)))
            {
                e.Handled = true;
                txtCheckRow.Text = e.KeyChar.ToString();
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((keyData == Keys.Enter))
            {
                if (string.IsNullOrEmpty(txtCheckRow.Text.Trim()))
                {
                    SelectCustom();
                    txtCheckRow.Text = "1";
                    txtCheckRow.Focus();
                    txtCheckRow.Select();
                }
                else
                {
                    int index = int.Parse(txtCheckRow.Text.Trim()) - 1;
                    int sumNum = dataGridView1.Rows.Count;
                    if (index >= sumNum)
                    {
                        MessageBox.Show("超出总行数了!");
                    }
                    else
                    {
                        string ID = this.dataGridView1.Rows[index].Cells[1].EditedFormattedValue.ToString().Trim();
                        this.Custom_ID = ID;
                        this.Close();
                    }
                }
                return base.ProcessDialogKey(keyData);
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelect_Click(object sender, EventArgs e)
        {
            SelectCustom();
            txtCheckRow.Text = "1";
            txtCheckRow.Focus();
            txtCheckRow.Select();
        }

        private void SelectCustom()
        {
            string tel = txtTel.Text.Trim();
            string mobile = txtMobile.Text.Trim();
            string name = txtName.Text.Trim();
            string bank = txtBank.Text.Trim();
            DataSet ds = CustomerDAO.GetCustomers(outletID, name, mobile, tel, bank);
            MainFormInitalize(dataGridView1, ds.Tables[0]);
        }

        private void txtbox_Enter(object sender, EventArgs e)
        {
            txtCheckRow.Text = "";
        }

        private void CheckUser_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.F && e.Control)
            {
                this.Close();
            }
        }

        private void txtTel_TextChanged(object sender, EventArgs e)
        {
            txtCheckRow.Text = "";
        }
    }
}
